<?php
include('config.php');
if(!isset($_SESSION['username']) || $_SESSION['admin'] != "T") {
    header("Location: index.php");
}
   
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Obstacle - Loophulpmiddelen bewoner beheren</title>
<link rel="stylesheet" href="stylemain.css"/>
<script src="modernizr-1.6.min.js"></script>
<script src="calendar.js"></script>
</head>

<body>
<div id="container">

<header>

<h2>Screeningsinstrument met bijhorende databanken<br>In samenwerking met <a href=\"http://www.phl.be\" target=\"_blank\">PHL</a> en <a href=\"http://www.woonlabo.be/\" target=\"_blank\">Woonlabo</a></h2>

<h1><a href="index.php">Obstacle</a></h1>

</header>

<?php
	require_once('menu.php');
?>

<article>

<?php
	require_once('login.php');
?>

<section>
<hgroup>
<h1>Loophulpmiddelen beheren</h1>
</hgroup>

     <?php if(!isset($_POST['Submit'])) { 
        
        
    ?>

<form  name="patientform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <table>
        <tr>
            <td>
                Loophulpmiddelen:
            </td>
            <td>
            <?php
                $usedaidid[] = array();
                $query1 = "SELECT * FROM patientusedaid WHERE PatientId=".$_GET['id']."";
                $result1 = mysql_query($query1) or die (mysql_error());
                while ($row1 = mysql_fetch_array($result1))
                {
                    $usedaidid[] = $row1['UsedAidId'];
                }
                
                $query2 = "SELECT * FROM usedaid WHERE Id>1";
                $result2 = mysql_query($query2) or die (mysql_error());
                while ($row2 = mysql_fetch_array($result2))
                {
                    if (in_array($row2['Id'], $usedaidid))
                    {
                        echo "<input checked type=\"checkbox\" id=\"loophulpmiddel[]\" name=\"loophulpmiddel[]\" value=\"".$row2['Id']."\"> ".$row2['Name']."<br>";
                    }
                    else
                    {
                        echo "<input type=\"checkbox\" id=\"loophulpmiddel[]\" name=\"loophulpmiddel[]\" value=\"".$row2['Id']."\"> ".$row2['Name']."<br>";
                    }


                }
            ?>
            </td>
        </tr>
        
 	<tr>
            <td><input type="submit" name="Submit" value="Aanpassen">
            <input type="hidden" name="patientid" value="<?php echo $_GET['id'] ?>" />
            <input type="hidden" name="search" value="<?php if (isset($_GET['search'])){ echo $_GET['search']; } else { echo "0"; }  ?>" />
            <input type="hidden" name="start" value="<?php if (isset($_GET['start'])){ echo $_GET['start']; } else { echo 0; }  ?>" /></td>
            <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
        </tr>
</table>
</form>
    <?php 
} 
else {
    
        $checkbox = $_POST['loophulpmiddel']; //from name="checkbox[]"
        $countCheck = count($_POST['loophulpmiddel']);
        
        $query3 = "SELECT * FROM patientusedaid WHERE PatientId=".$_POST['patientid']." AND UsedAidId=1";
        $result3 = mysql_query($query3) or die (mysql_error());
        while ($row3 = mysql_fetch_array($result3))
        {
            $id = $row3['Id'];
        }
        
        mysql_query("UPDATE patientmovementrestriction SET PatientUsedAidId=".$id." WHERE PatientId=".$_POST['patientid']."");
        mysql_query("DELETE FROM patientusedaid WHERE PatientId=".$_POST['patientid']." AND UsedAidId>1");

        if ($countCheck >= 1)
        {
            for($i=0;$i<$countCheck;$i++)
            {
                    $aid_id  = $checkbox[$i];
                    $sql = "INSERT INTO patientusedaid (PatientId, UsedAidId) VALUES (".$_POST['patientid'].",".$aid_id.")";
                    $result = mysql_query($sql) or die (mysql_error());
            }
            if($result)
            {	
                    echo "<p>De loophulpmiddelen voor deze bewoner zijn aangepast.</p>";
                    if ($_POST['start'] == 0)
                    {
                        echo "Klik <a href=\"patientaanpassen.php?id=".$_POST['patientid']."\">hier</a> om terug te gaan naar de aangepaste bewoner.";
                    }
                    else
                    {
                        echo "Klik <a href=\"patientaanpassen.php?id=".$_POST['patientid']."&start=".$_POST['start']."\">hier</a> om terug te gaan naar de aangepaste bewoner.";
                    }
                    
            }
            else
            {
                    echo "Error: ".mysql_error();
            }
        }
}
?>

</section>

</article>
<footer>

<h1>&copy; Copyright 2012 Obstacle</h1>

<h2>
Design by <a href="http://www.designity.org/">Free Web Design Community</a>
</h2>

</footer>

</div>
</body>
</html>
